package tests;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import fi.helsinki.cs.kaisei.Schedule;
import fi.helsinki.cs.kaisei.Timetable;
import junit.framework.TestCase;

public class ScheduleTest extends TestCase {
    
    private Schedule schedule;
    private ArrayList<Timetable> timetables;
    
    @Before
    public void setUp() throws Exception {
        timetables = new ArrayList<Timetable>();
        timetables.add(new Timetable(51,2));
        timetables.add(new Timetable(25,26));
    }

    @After
    public void tearDown() throws Exception {
        schedule = null;
        timetables = null;
    }
    
    @Test
    public void testAllConstructors() {
        schedule = new Schedule("someperiod");
        
        assertNotNull(schedule);
        
        schedule = null;
        
        assertNull(schedule);
        
        schedule = new Schedule(timetables,"someperiod");
        
        assertNotNull(schedule);
        
        assertEquals(timetables,schedule.getTimetables());
        
        schedule = null;
        
        assertNull(schedule);
    }
    
    @Test
    public void testSetSchedule() {
        schedule = new Schedule("someperiod");
        Schedule otherschedule = new Schedule(timetables,"someentirelydifferentperiod");
        
        schedule.setSchedule(otherschedule);
        assertEquals(schedule.getPeriod(), otherschedule.getPeriod());
        assertEquals(schedule.getTimetables(), otherschedule.getTimetables());
    }
    
    @Test
    public void testSetPeriod() {
        String period = "first period";
        schedule = new Schedule(period);
    
        assertEquals(period, schedule.getPeriod());
        
        period = "totally another period";
        schedule.setPeriod(period);
        assertEquals(period, schedule.getPeriod());
    }
    
    @Test
    public void testAddTimetable() {
        schedule = new Schedule("someperiod");
        schedule.addTimetable(timetables.get(0));
        schedule.addTimetable(timetables.get(1));
        
        assertEquals(timetables.get(0).getStartWeek(), schedule.getTimetables().get(0).getStartWeek());
        assertEquals(timetables.get(0).getEndWeek(), schedule.getTimetables().get(0).getEndWeek());
        assertEquals(timetables.get(1).getStartWeek(), schedule.getTimetables().get(1).getStartWeek());
        assertEquals(timetables.get(1).getEndWeek(), schedule.getTimetables().get(1).getEndWeek());
    }
    
    @Test
    public void testSearchTimetables() {
        schedule = new Schedule(timetables,"someperiod");
        ArrayList<Timetable> test;
        
        test = schedule.searchTimetables(27, 50);
        assertFalse(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(50, 52);
        assertTrue(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(52, 1);
        assertTrue(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(1, 3);
        assertTrue(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(50, 3);
        assertTrue(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(3, 24);
        assertFalse(test.contains(timetables.get(0)));
        assertFalse(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(24, 26);
        assertFalse(test.contains(timetables.get(0)));
        assertTrue(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(25, 26);
        assertFalse(test.contains(timetables.get(0)));
        assertTrue(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(25, 27);
        assertFalse(test.contains(timetables.get(0)));
        assertTrue(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(24, 27);
        assertFalse(test.contains(timetables.get(0)));
        assertTrue(test.contains(timetables.get(1)));
        
        test = schedule.searchTimetables(26, 25);
        assertTrue(test.contains(timetables.get(0)));
        assertTrue(test.contains(timetables.get(1)));
    }
    
}
